package com.ls.lserp.modules.purchase.service;


import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.material.entity.InventoryRequest;
import com.ls.lserp.modules.purchase.dao.PuttingRequestDao;
import com.ls.lserp.modules.purchase.entity.PuttingRequest;

/**
 * @ClassName: InventoryRequestService
 * @Description: 物料入库申请
 * 
 * @author 
 * @date 2017年04月14日
 *
 */

@Service
@Transactional(readOnly = false)
public class PuttingRequestService extends CrudService<PuttingRequestDao,PuttingRequest>{

    @Autowired
    private PuttingRequestDao puttingRequestDao;
    
	/**
	 * @Title: findList
	 * @Description: 一览查询
	 * @param page
	 * @param orderMateriel
	 * @return Page<OrderMateriel>
	 */
	public Page<PuttingRequest> findList(Page<PuttingRequest> page, PuttingRequest puttingRequest) {
		
		
		// tab页分，检索条件设定(0:全部,1:待出库,2:已出库)
		puttingRequest.setInventoryStatus(ResourcesUtil.getConfig("outbound_status_wait"));
		// 出库类别（外发出库）
		puttingRequest.setInventoryCategory(ResourcesUtil.getConfig("outbound_category_putting"));
		
		// 设置分页参数
		puttingRequest.setPage(page);
		List<PuttingRequest> resultList = puttingRequestDao.findList(puttingRequest);
        // 执行分页查询
        page.setList(resultList);
        
        return page;
	}
	
	/**
	 * check BigDecimal
	 * @param cost
	 * @return 取不到返回0
	 */
	public BigDecimal checkBigDecimalNull(BigDecimal cost){
		if(cost==null){
			return new BigDecimal("0");
		}
		return cost;
	}
	
    /**
     * @Description: 查询
     * @param @return
     * @return InventoryRequestEx
     * @throws
     */
   public PuttingRequest getPuttingContractInfo(PuttingRequest puttingRequest) {
	   List<PuttingRequest> findMaterial = puttingRequestDao.findMaterial(puttingRequest);
	   puttingRequest = puttingRequestDao.getPuttingContractInfo(puttingRequest);
	   puttingRequest.setRequestDetailsList(findMaterial);
       return puttingRequest;
   }
   
   /**
    * @Title: findBrand
    * @Description: 查询
    * @param @param venderId
    * @param @return
    * @return List<InventoryRequestEx>
    * @throws
    */
  public List<PuttingRequest> findPuttingContractList(PuttingRequest puttingRequest) {
      return puttingRequestDao.findPuttingContractList(puttingRequest);
  }
  /**
   * @Title: findBrand
   * @Description: 查询
   * @param @param venderId
   * @param @return
   * @return List<InventoryRequestEx>
   * @throws
   */
 public List<PuttingRequest> findStyleList(PuttingRequest puttingRequest) {
     return puttingRequestDao.findStyleList(puttingRequest);
 }
  /**
   * @Title: findBrand
   * @Description: 查询
   * @param @param venderId
   * @param @return
   * @return List<InventoryRequestEx>
   * @throws
   */
 public List<PuttingRequest> findFabricatingList() {
     return puttingRequestDao.findFabricatingList();
 }
  
  /**
   * @Title: saveInventoryRequest
   * @Description: 保存入库申请
   * @param @param inventoryRequest
   * @return void 
   * @throws
   */
  @Transactional(readOnly = false)
  public void savePuttingRequest(PuttingRequest puttingRequest) {
  	
  	// 判断新建编辑
  	//若为null 执行插入语句（新建）
  	if (puttingRequest.getId()==null){			
  		puttingRequest.preInsert();
  		puttingRequest.setVersion(1L);
  		puttingRequest.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
  		puttingRequest.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
  		puttingRequest.setInventoryStatus(ResourcesUtil.getConfig("outbound_status_wait"));
  		puttingRequest.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
  		puttingRequestDao.insertInventoryRequestEX(puttingRequest);
  	}else{
		// 更新数据
  		puttingRequest.preUpdate();
  		puttingRequestDao.updateInventoryRequestEx(puttingRequest);
		// 删除入库单明细
  		puttingRequestDao.deleteRequestDetails(puttingRequest);
  	}
  	
  	
  	// 判断list是否为空
  	if(!CollectionsUtils.isEmpty(puttingRequest.getRequestDetailsList())){
  	   // 遍历赋值
         for(PuttingRequest itr : puttingRequest.getRequestDetailsList()){
        	 itr.setId(puttingRequest.getId());
        	 itr.preInsert();
        	 itr.setVersion(1L);
        	 itr.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
        	 itr.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
        	 itr.setStockinStatus(ResourcesUtil.getConfig("outbound_status_wait"));
        	 puttingRequestDao.insertInventoryRequestDetails(itr);
  		}
  	}
  }
  
  /**
   * @Title: findRequestDetailsList
   * @Description: 入库申请明细
   * @param @param inventoryRequest
   * @param @return
   * @return List<InventoryRequest>
   * @throws
   */
 
 public List<PuttingRequest> findRequestDetail(PuttingRequest puttingRequest) {
     return puttingRequestDao.findRequestDetail(puttingRequest);
 }
}